﻿var AccountValidationClientSideHide = false;
var CurrentTab = 1;
var LineNoArray = [];
var ProfileLineNoArray = [];
var CurrentTabElement = null;
var SelectedLCountryID = null;
var UserIDView = 0;
var ProfileIDView = "0";
var ProfileIDViewChange = "0";
var SelectedIDView = "0";
var IsDeleteView = false;
var LoadedToolTabColumnPermissionGrid = false;
var HomePhone = null;
var DataMessage = "";
var ProfileNameText = "";
var EmrgcyContactPhone = null;
var ProfileToolDropJs = 0;
var ToolDropJs = 0;
var ProfileToolTabDropJs = 0;
var ToolTabDropJs = 0;
var GridElementsChange = false;
var ProfileNameChange = false;
$(document).ready(function () {
    var path = window.location.pathname;
    if (path.indexOf("ManageUsers") != -1
        || path.indexOf("CreateUser") != -1
        || path.indexOf("EditUser") != -1
        || path.indexOf("Login") != -1) {
        $.validator.addMethod("ManageUserRequireds", $.validator.methods.required, Messages["ManageUserRequireds"]);
        $.validator.addMethod("ManageUserDate", $.validator.methods.datePicker, Messages["ManageUserDate"]);
        $.validator.addMethod("ManageUserEmail", $.validator.methods.email, Messages["ManageUserEmail"]);
        $.validator.addMethod("ManageUserMinLength4", $.validator.methods.minlength, Messages["ManageUserMinLength4"]);
        $.validator.addMethod("ManageUserMaxLength4", $.validator.methods.maxlength, Messages["ManageUserMaxLength4"]);
        $.validator.addMethod("ManageUserMaxLength20", $.validator.methods.maxlength, Messages["ManageUserMaxLength20"]);
        $.validator.addMethod("ManageUserMaxLength30", $.validator.methods.maxlength, Messages["ManageUserMaxLength30"]);
        $.validator.addMethod("ManageUserMaxLength50", $.validator.methods.maxlength, Messages["ManageUserMaxLength50"]);
        $.validator.addMethod("ManageUserMaxLength100", $.validator.methods.maxlength, Messages["ManageUserMaxLength100"]);
        $.validator.addClassRules({
            Required: {
                ManageUserRequireds: true,
            },
            RequiredNoMessage: {
                CreateUserRequireds: true,
            },
            Email: {
                ManageUserEmail: true,
            },
            Date: {
                ManageUserDate: true,
            },
            MaxLength100: {
                ManageUserMaxLength100: 100,
            },
            MaxLength50: {
                ManageUserMaxLength50: 50,
            },
            MaxLength30: {
                ManageUserMaxLength30: 30,
            },
            MaxLength20: {
                ManageUserMaxLength20: 20,
            },
            MaxLength4: {
                ManageUserMaxLength4: 4,
            },
            MinLength4: {
                ManageUserMinLength4: 4,
            },
        });
        $('#LoadUserDetailsForm').validate();
        $('#LoginForm').validate();
        $('#CreateUserForm').validate();
        $('#LoadProfilePermissionsForm').validate();
        if (path.indexOf("ManageUsers") != -1) {
            LoadUserGrid();
        }
        ProfileIDView = "0";
        GridElementsChange = false;
        ProfileNameChange = false;
        $(".Tab").click(function (event) {

            if ($(this).hasClass("active")) {
                return false;
            }
            if ($(this).hasClass("Tab")) {
                CurrentTabElement = this;
            }
            if ((CurrentTab == 1 && !($('#ToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed'))) || (CurrentTab == 2 && !($('#ProfileToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed')))) {
                switch (CurrentTab) {
                    case 1:
                        {
                            $("#Permissions").removeClass("active");
                            $("#tabPermissions").removeClass("active in");
                            AccountValidationClientSideHide = false;
                        }
                        break;
                    case 2:
                        {
                            $("#Profiles").removeClass("active");
                            $("#tabProfiles").removeClass("active in");
                            AccountValidationClientSideHide = false;
                        }
                        break;
                    default:
                        break;
                }
                switch ($(CurrentTabElement).attr("id")) {
                    case "Permissions":
                        {
                            CurrentTab = 1;
                            $("#Permissions").addClass("active");
                            $("#tabPermissions").addClass("active in");
                            AccountValidationClientSideHide = false;
                        }
                        break;
                    case "Profiles":
                        {
                            CurrentTab = 2;
                            $("#Profiles").addClass("active");
                            $("#tabProfiles").addClass("active in");
                            AccountValidationClientSideHide = false;
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
            event.preventDefault();
            event.stopImmediatePropagation();
            if ($('#ToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed') && CurrentTab == 1) {
                jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
                    if (retVal != true) {
                        event.stopImmediatePropagation();
                        event.preventDefault();
                    }
                    else {
                        $("#Permissions").removeClass("active");
                        $("#tabPermissions").removeClass("active in");
                        switch ($(CurrentTabElement).attr("id")) {
                            case "Profiles":
                                {
                                    CurrentTab = 2;
                                    $("#Profiles").addClass("active");
                                    $("#tabProfiles").addClass("active in");
                                    AccountValidationClientSideHide = false;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                });
            }
            else {
                jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
                    if (retVal != true) {
                        event.stopImmediatePropagation();
                        event.preventDefault();
                    }
                    else {
                        $("#Profiles").removeClass("active");
                        $("#tabProfiles").removeClass("active in");
                        switch ($(CurrentTabElement).attr("id")) {
                            case "Permissions":
                                {
                                    CurrentTab = 1;
                                    $("#Permissions").addClass("active");
                                    $("#tabPermissions").addClass("active in");
                                    AccountValidationClientSideHide = false;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                });
            }
        });
    }
});
function ShowUserDetails(EmployeeID) {
    if (EmployeeID == "") {
        jAlert("There are no details for this user:", "Error");
        return;
    }
    $.ajax({
        url: urls["AccountLoadUserDetails"],
        data: { 'EmployeeID': EmployeeID },
        type: 'GET',
        success: function (data) {
            $("#UserDetailsDiv").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            SelectedLCountryID = $("#SelectedLCountryID").val();
            HomePhone = $("#HomePhone").val();
            EmrgcyContactPhone = $("#EmrgcyContactPhone").val();
            FormattedPhoneNumbers(SelectedLCountryID);
            ShowUserDetailPopUp("UserDetailsDiv");
            InitUserDetailsDataCalendars();
        }
    });
}
function ShowUserDetailPopUp(PopUpDiv) {
    $.colorbox({
        width: "1000px",
        height: "700px",
        inline: true,
        href: "#" + PopUpDiv,
        onComplete: function () {
            $(".phone").mask("(###) ###-####");
            $(".phoneUSACAN").mask("+1 (###) ###-####");
            $(".phoneTR").mask("+90 (###) ###-####");
            $(".phoneFR").mask("+33 (#) ##-##-##-##");
            $(".phoneBEL").mask("+32 (#) ###-##-##");
            $(".phoneSWI").mask("+41 (##) ###-####");
            $(".phoneTS").mask("+216 (##) ###-###");
            $(".phoneALG").mask("+213 (##) ###-####");
            $(".phoneMA").mask("+212 (##) ###-####");
            $(".SSNumber").mask("###-##-####");
            $(".PostalCode").mask("#####-####");
            $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });
            $("#" + PopUpDiv).show();
        },
        onClosed: function () {
            $("#" + PopUpDiv).hide();
        }
    });
}
$(document).on('change', '.UserDetailsElementChange', function () {
    IsDeleteView = false;
    if (!$('#UserDetailsSaveBtn').hasClass("disabled")) {
        $('#UserDetailsSaveBtn').addClass('BackColorRed');
    }
});
function InitUserDetailsDataCalendars() {
    $(".UserDeteilsDataCalendar").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat: 'dd/M/yy',
        beforeShow: function (input) {
            $(input).css({
                "position": "relative",
                "z-index": 999999
            });
        }
    });
}
$(document).on('click', '#UserDetailsSaveBtn', function () {
    var $form = $('#LoadUserDetailsForm');
    if ($form.valid()) {
        if ($('#UserQuotRegions input[name=SelectedQuotaRegions]:checked').length > 0) {
            AccountValidationClientSideHide = false;
            $.ajax({
                url: $form.attr('action'),
                type: $form.attr('method'),
                data: $form.serialize(),
                beforeSend: function (xhr) {
                    xhr.overrideMimeType('text/plain; charset=x-user-defined');
                },
                success: function (data) {
                    if (data.indexOf("Error") != -1)
                        jAlert(data, "Error");
                },
                error: function (xhr) {
                    jAlert(xhr.statusText, "Error");
                },
                complete: function () {
                    $('#UserDetailsSaveBtn').removeClass('BackColorRed')
                    jAlert("Data SuccessFully Saved:", "Successful");
                }
            });
        }
        else
            jAlert("Please Select QuotaRegion:", "Error!");
    }
    else {
        AccountValidationClientSideHide = true;
        $("#errorMessageServer").hide();
    }
});

$(document).on('click', '#LoginBtn', function () {
    var $form = $('#LoginForm');
    if ($form.valid()) {
        AccountValidationClientSideHide = false;
    }
    else {
        AccountValidationClientSideHide = true;
        $("#errorMessageServer").hide();
    }
});
$(document).on('click', '#CreateUserBtn', function () {
    var $form = $('#CreateUserForm');
    if ($form.valid()) {
        AccountValidationClientSideHide = false;
    }
    else {
        AccountValidationClientSideHide = true;
        $("#errorMessageServer").hide();
    }
});
$(document).on('click', '#EditUserBtn', function () {
    var $form = $('#CreateUserForm');
    if ($form.valid()) {
        AccountValidationClientSideHide = false;
    }
    else {
        AccountValidationClientSideHide = true;
        $("#errorMessageServer").hide();
    }
});
function ShowToolTabColumnPermissionsAll(UserID) {
    ShowToolTabColumnPermissions(UserID);
    ShowProfileToolTabColumnPermissions();
    CurrentTab = 1;
    $("#Permissions").addClass("active");
    $("#tabPermissions").addClass("active in");
}
function ShowToolTabColumnPermissions(UserID) {
    $.ajax({
        url: urls["AccountLoadToolTabColumnPermissions"],
        data: { 'UserID': UserID },
        type: 'GET',
        success: function (data) {
            $("#tabPermissions").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error")
        },
        complete: function () {
            $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });
            ShowToolTabColumnPermissionsPopUp("ToolDiv");
        },
    });
}
function ShowProfileToolTabColumnPermissions() {
    $.ajax({
        url: urls["AccountLoadProfileToolTabColumnPermissions"],
        type: 'GET',
        success: function (data) {
            $("#tabProfiles").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error")
        },
        complete: function () {
            $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });
            $("#ProfileName").val($('option:selected', $("#ProfileSelectedProfile")).text());
            ProfileIDView = $("#ProfileSelectedProfile").val();
            $("#ProfileID").val(ProfileIDView);
            LoadedToolTabColumnPermissionGrid = false;
        },
    });
}
function ShowToolTabColumnPermissionsPopUp(PopUpDiv) {
    $.colorbox({
        width: "700px",
        height: "700px",
        inline: true,
        href: "#" + PopUpDiv,
        onComplete: function () {
            $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });
            $("#" + PopUpDiv).show();
        },
        onClosed: function () {
            $("#" + PopUpDiv).hide();
        }
    });
}
$(document).on('change', '.LineNoChangeDrop', function () {
    $('#ToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
    var td = $(this).parent();
    var tr = td.parent();
    var CanViewDivContent = tr.find(".CanViewDivForChange").contents();
    var CanEditDivContent = tr.find(".CanEditDivForChange").contents();
    var HasFilterDivContent = tr.find(".HasFilterDivForChange").contents();
    tr.find(".CanViewDivForChange").empty();
    tr.find(".CanEditDivForChange").empty();
    tr.find(".HasFilterDivForChange").empty();
    var tbody = tr.parent();
    var DropDowns = tbody.find("Select.LineNoChangeDropHide");
    for (var i = 0; i < DropDowns.length; ++i) {
        if ($(DropDowns.get(i)).val() == $(this).val()) {
            var changeelementtr = $(DropDowns.get(i)).parent().parent();
            var changeCanViewDivContent = changeelementtr.find(".CanViewDivForChange").contents();
            var changeCanEditDivContent = changeelementtr.find(".CanEditDivForChange").contents();
            var changeHasFilterDivContent = changeelementtr.find(".HasFilterDivForChange").contents();
            changeelementtr.find(".CanViewDivForChange").empty();
            changeelementtr.find(".CanEditDivForChange").empty();
            changeelementtr.find(".HasFilterDivForChange").empty();
            tr.find(".CanViewDivForChange").html(changeCanViewDivContent);
            tr.find(".CanEditDivForChange").html(changeCanEditDivContent);
            tr.find(".HasFilterDivForChange").html(changeHasFilterDivContent);
            changeelementtr.find(".CanViewDivForChange").html(CanViewDivContent);
            changeelementtr.find(".CanEditDivForChange").html(CanEditDivContent);
            changeelementtr.find(".HasFilterDivForChange").html(HasFilterDivContent);
            $(DropDowns.get(i)).prev().val($(this).next().val());
            break;
        }
    }
    BindToolTabColumnName();
});
$(document).on('change', '.ProfileLineNoChangeDrop', function () {
    $('#ProfileToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
    var td = $(this).parent();
    var tr = td.parent();
    var CanViewDivContent = tr.find(".ProfileCanViewDivForChange").contents();
    var CanEditDivContent = tr.find(".ProfileCanEditDivForChange").contents();
    var HasFilterDivContent = tr.find(".ProfileHasFilterDivForChange").contents();
    tr.find(".ProfileCanViewDivForChange").empty();
    tr.find(".ProfileCanEditDivForChange").empty();
    tr.find(".ProfileHasFilterDivForChange").empty();
    var tbody = tr.parent();
    var DropDowns = tbody.find("Select.ProfileLineNoChangeDropHide");
    for (var i = 0; i < DropDowns.length; ++i) {
        if ($(DropDowns.get(i)).val() == $(this).val()) {
            var changeelementtr = $(DropDowns.get(i)).parent().parent();
            var changeCanViewDivContent = changeelementtr.find(".ProfileCanViewDivForChange").contents();
            var changeCanEditDivContent = changeelementtr.find(".ProfileCanEditDivForChange").contents();
            var changeHasFilterDivContent = changeelementtr.find(".ProfileHasFilterDivForChange").contents();
            changeelementtr.find(".ProfileCanViewDivForChange").empty();
            changeelementtr.find(".ProfileCanEditDivForChange").empty();
            changeelementtr.find(".ProfileHasFilterDivForChange").empty();
            tr.find(".ProfileCanViewDivForChange").html(changeCanViewDivContent);
            tr.find(".ProfileCanEditDivForChange").html(changeCanEditDivContent);
            tr.find(".ProfileHasFilterDivForChange").html(changeHasFilterDivContent);
            changeelementtr.find(".ProfileCanViewDivForChange").html(CanViewDivContent);
            changeelementtr.find(".ProfileCanEditDivForChange").html(CanEditDivContent);
            changeelementtr.find(".ProfileHasFilterDivForChange").html(HasFilterDivContent);
            $(DropDowns.get(i)).prev().val($(this).next().val());
            break;
        }
    }
    BindProfileToolTabColumnName();
});
$(document).on('change', '.ToolTabDrop', function () {
    var $ToolTabDropLoc = $(this)
    if ($('#ToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
            if (retVal == true) {
                if ($ToolTabDropLoc.val() === "0" || $ToolTabDropLoc.val() === "") {
                    $("#ToolTabColumnPermissionsGridDiv").empty();
                    $("#ToolTabColumnPermissionsGridDiv").hide();
                }
                else
                    ShowToolTabColumnPermissionsGrid($ToolTabDropLoc.val(), $("#SelectedTool").val());
                ToolTabDropJs = $ToolTabDropLoc.val();
                $('#ToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed')
            }
            else
            {
                $ToolTabDropLoc.val(ToolTabDropJs);
            }
        });
    }
    else {
        if ($ToolTabDropLoc.val() === "0" || $ToolTabDropLoc.val() === "") {
            $("#ToolTabColumnPermissionsGridDiv").empty();
            $("#ToolTabColumnPermissionsGridDiv").hide();
        }
        else
            ShowToolTabColumnPermissionsGrid($ToolTabDropLoc.val(), $("#SelectedTool").val());
        ToolTabDropJs = $ToolTabDropLoc.val();
    }
});
$(document).on('change', '.ProfileToolTabDrop', function () {
    var $ProfileToolTabDropLoc = $(this)
    if ($('#ProfileToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed') && GridElementsChange) {
        jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
            if (retVal == true) {
                if ($ProfileToolTabDropLoc.val() === "0" || $ProfileToolTabDropLoc.val() === "") {
                    $("#ProfileToolTabColumnPermissionsGridDiv").empty();
                    $("#ProfileToolTabColumnPermissionsGridDiv").hide();
                    $("#CopyAllProfile").hide();
                    LoadedToolTabColumnPermissionGrid = false;
                }
                else
                    ShowProfileToolTabColumnPermissionsGrid($ProfileToolTabDropLoc.val(), $("#SelectedProfileTool").val());
                ProfileToolTabDropJs = $ProfileToolTabDropLoc.val();
                if (!ProfileNameChange) {
                    $('#ProfileToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                }
                GridElementsChange = false;
            }
            else {
                $ProfileToolTabDropLoc.val(ProfileToolTabDropJs);
            }
        });
    }
    else {
        if ($ProfileToolTabDropLoc.val() === "0" || $ProfileToolTabDropLoc.val() === "") {
            $("#ProfileToolTabColumnPermissionsGridDiv").empty();
            $("#ProfileToolTabColumnPermissionsGridDiv").hide();
            $("#CopyAllProfile").hide();
            LoadedToolTabColumnPermissionGrid = false;
        }
        else
            ShowProfileToolTabColumnPermissionsGrid($ProfileToolTabDropLoc.val(), $("#SelectedProfileTool").val());
        ProfileToolTabDropJs = $ProfileToolTabDropLoc.val();
    }
});
function ShowToolTabColumnPermissionsGrid(ToolTabID, ToolID) {
    $.ajax({
        url: urls["AccountLoadToolTabColumnPermissionsGrid"],
        data: {
            'UserID': $("#UserID").val(),
            'ToolTabID': parseInt(ToolTabID),
            'ToolID': parseInt(ToolID),
        },
        type: 'GET',
        success: function (data) {
            $("#ToolTabColumnPermissionsGridDiv").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error")
        },
        complete: function () {
            if (parseInt(ToolID) == 2)
            {
                LineNoArray = [];
                $(".LineNo").each(function (index, elem) {
                    LineNoArray.push($(elem).val())
                });
            }
            $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });
            $("#ToolTabColumnPermissionsGridDiv").show();
            MakeDisabledHasFilterCanEdit();
            BindToolTabColumnName();
            BindCheckBoxHeader("CanView");
            BindCheckBoxHeader("CanEdit");
            BindCheckBoxHeader("HasFilter");
        }
    });
}
function ShowProfileToolTabColumnPermissionsGrid(ToolTabID, ToolID) {
    $.ajax({
        url: urls["AccountLoadProfileToolTabColumnPermissionsGrid"],
        data: {
            'ProfileName': $("#ProfileName").val(),
            'ProfileID': parseInt(ProfileIDView),
            'ToolTabID': parseInt(ToolTabID),
            'ToolID': parseInt(ToolID),
        },
        type: 'GET',
        success: function (data) {
            $("#ProfileToolTabColumnPermissionsGridDiv").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error")
        },
        complete: function () {
            if (parseInt(ToolID) == 2) {
                ProfileLineNoArray = [];
                $(".ProfileLineNo").each(function (index, elem) {
                    ProfileLineNoArray.push($(elem).val())
                });
                $("#CopyAllProfile").show();
            }
            $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });
            $("#ProfileToolTabColumnPermissionsGridDiv").show();
            ProfileMakeDisabledHasFilterCanEdit();
            BindProfileToolTabColumnName();
            LoadedToolTabColumnPermissionGrid = true;
            BindProfileCheckBoxHeader("ProfileCanView");
            BindProfileCheckBoxHeader("ProfileCanEdit");
            BindProfileCheckBoxHeader("ProfileHasFilter");
        }
    });
}
$(document).on('click', '#ProfileApplyBtn', function () {
    if ($('#ToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
            if (retVal == true) {
                ApplyProfile($("#UserID").val(), $("#SelectedProfile").val());
                $('#ToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed')
            }
        });
    }
    else {
        ApplyProfile($("#UserID").val(), $("#SelectedProfile").val());
    }
});
function ApplyProfile(UserID, ProfileID) {
    $.ajax({
        url: urls["AccountApplyProfile"],
        data: {
            'UserID': parseInt(UserID),
            'ProfileID': parseInt(ProfileID),
        },
        type: 'POST',
        success: function (data) {
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error")
        },
        complete: function () {
            ShowToolTabColumnPermissions(UserID);
            UpdateProfiles(parseInt(ProfileIDView));
            jAlert("Profile SuccessFully Applied:", "Successful");
        }
    });
}
function DeleteUser(UserID) {
    jConfirm('Are you sure you want to delete this User?', 'Warning!', function (retVal) {
        if (retVal == true) {
            UserIDView = UserID;
            IsDeleteView = true;
            LoadUserGrid();
        }
    });
};
$(document).on('change', '.ToolElemnetsChange', function () {
    $('#ToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
});
$(document).on('change', '.ProfileToolElemnetsChange', function () {
    $('#ProfileToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
});
$(document).on('change', '.ProfileNameChange', function () {
    ProfileNameChange = true;
});
$(document).on('change', '.GridElementsChange', function () {
    GridElementsChange = true;
});
function ValidToolTabColumnPermisionsCanViewCanEditHasFilterData()
{
    var valid = true
    var LineNumbers = [];
    $(".LineNo").each(function (index, elem) {
        LineNumbers.push($(elem).val());
    });
    var LineNoCount = LineNumbers.length;
    var maxLineNo = Math.max.apply(Math, LineNumbers);
    var minLineNo = Math.min.apply(Math, LineNumbers);
    var UniqueLineNo = $.unique(LineNumbers).length;
    if (UniqueLineNo != LineNoCount)
    {
        jAlert("LineNo must not have duplicate values", "Error!");
        valid = false;

    }
    else if (minLineNo < 1) {
        jAlert("LineNo can`t be empty", "Error!");
        valid = false;
    }
    else if (maxLineNo > LineNumbers.length)
    {
        jAlert("Max LineNo can`t be greater then element count. LineNo count = " + LineNumbers.length.toString(), "Error!");
        valid = false;
    }
    return valid;
}
function ValidProfileToolTabColumnPermisionsCanViewCanEditHasFilterData() {
    var Profilevalid = true
    var ProfileLineNumbers = [];
    $(".ProfileLineNo").each(function (index, elem) {
        ProfileLineNumbers.push($(elem).val());
    });
    var ProfileLineNoCount = ProfileLineNumbers.length;
    var ProfilemaxLineNo = Math.max.apply(Math, ProfileLineNumbers);
    var ProfileminLineNo = Math.min.apply(Math, ProfileLineNumbers);
    var ProfileUniqueLineNo = $.unique(ProfileLineNumbers).length;
    if (ProfileUniqueLineNo != ProfileLineNoCount) {
        jAlert("LineNo must not have duplicate values", "Error!");
        Profilevalid = false;

    }
    else if (ProfileminLineNo < 1) {
        jAlert("LineNo can`t be empty", "Error!");
        Profilevalid = false;
    }
    else if (ProfilemaxLineNo > ProfileLineNumbers.length) {
        jAlert("Max LineNo can`t be greater then element count. LineNo count = " + ProfileLineNumbers.length.toString(), "Error!");
        Profilevalid = false;
    }
    if (Profilevalid)
    {
        ProfileLineNoArray = []
        $(".ProfileLineNo").each(function (index, elem) {
            ProfileLineNoArray.push($(elem).val())
        });
    }
    return Profilevalid;
}
$(document).on('click', '#ToolTabColumnPermissionsSaveBtn', function () {
    switch (parseInt($("#ToolID").val()))
    {
        case 1:
            {
                if ($("#ToolTabID").val() == 17) {
                    var ToolTabColumnPermisionsCanViewCanEditGridViewModel = {
                        Rows: [],
                    };
                    var ind = 0;
                    $(".LineNoChangeDropHide").each(function (index, elem) {
                        var trparent = $(elem).parent();
                        var tdparent = trparent.parent();
                        var View = $(tdparent.find("input.CanView:checkbox")).prop("checked");
                        var Edit = $(tdparent.find("input.CanEdit:checkbox")).prop("checked");
                        var SaveRows = {
                            UserID: $("#UserID").val(),
                            ProfileID: 0,
                            ToolTabID: $("#ToolTabID").val(),
                            SelectedToolTabColumnID: parseInt($(elem).val()),
                            CanView: View,
                            CanEdit: Edit,
                        };
                        ToolTabColumnPermisionsCanViewCanEditGridViewModel.Rows.push(SaveRows);
                        ++ind;
                    });
                    var DTO = JSON.stringify(ToolTabColumnPermisionsCanViewCanEditGridViewModel.Rows);
                    console.log(DTO);
                    $.ajax({
                        url: urls["AccountSaveToolTabColumnPermisionsCanViewCanEditData"],
                        type: 'POST',
                        contentType: 'application/json; charset=utf-8',
                        data: DTO,
                        success: function (data) {
                            if (data.indexOf("Error") != -1)
                                jAlert(data, "Error");
                        },
                        error: function (xhr) {
                            jAlert(xhr.statusText, "Error");
                        },
                        complete: function () {
                            $('#ToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                            jAlert("Data SuccessFully Saved:", "Successful");
                        }
                    });
                }
                else {
                    var ToolTabColumnPermisionsCanViewGridViewModel = {
                        Rows: [],
                    };
                    var ind = 0;
                    $(".LineNoChangeDropHide").each(function (index, elem) {
                        var trparent = $(elem).parent();
                        var tdparent = trparent.parent();
                        var View = $(tdparent.find("input.CanView:checkbox")).prop("checked");
                        var SaveRows = {
                            UserID: $("#UserID").val(),
                            ProfileID: 0,
                            ToolTabID: $("#ToolTabID").val(),
                            SelectedToolTabColumnID: parseInt($(elem).val()),
                            CanView: View,
                        };
                        ToolTabColumnPermisionsCanViewGridViewModel.Rows.push(SaveRows);
                        ++ind;
                    });
                    var DTO = JSON.stringify(ToolTabColumnPermisionsCanViewGridViewModel.Rows);
                    console.log(DTO);
                    $.ajax({
                        url: urls["AccountSaveToolTabColumnPermisionsCanViewData"],
                        type: 'POST',
                        contentType: 'application/json; charset=utf-8',
                        data: DTO,
                        success: function (data) {
                            if (data.indexOf("Error") != -1)
                                jAlert(data, "Error");
                        },
                        error: function (xhr) {
                            jAlert(xhr.statusText, "Error");
                        },
                        complete: function () {
                            $('#ToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                            jAlert("Data SuccessFully Saved:", "Successful");
                        }
                    });
                }
            }
            break;
        case 2:
            {
                if (ValidToolTabColumnPermisionsCanViewCanEditHasFilterData()) {
                    var ToolTabColumnPermisionsCanViewCanEditHasFilterGridViewModel = {
                        Rows: [],
                    };
                    var ind = 0;
                    $(".LineNoChangeDropHide").each(function (index, elem) {
                        var trparent = $(elem).parent();
                        var tdparent = trparent.parent();
                        var LineNum = $(tdparent.find(".LineNo")).val();
                        var View = $(tdparent.find("input.CanView:checkbox")).prop("checked");
                        var Edit = $(tdparent.find("input.CanEdit:checkbox")).prop("checked");
                        var Filter = $(tdparent.find("input.HasFilter:checkbox")).prop("checked");
                        var SaveRows = {
                            UserID: $("#UserID").val(),
                            ProfileID: 0,
                            ToolTabFixedColumnCountID: $("#ToolTabFixedColumnCountID").val(),
                            ToolTabID: $("#ToolTabID").val(),
                            FixedColumnCount: $("#FixedColumnCount").val(),
                            SelectedToolTabColumnID: parseInt($(elem).val()),
                            LineNo: parseInt(LineNum),
                            CanView: View,
                            CanEdit: Edit,
                            HasFilter: Filter,
                        };
                        ToolTabColumnPermisionsCanViewCanEditHasFilterGridViewModel.Rows.push(SaveRows);
                        ++ind;
                    });
                    var DTO = JSON.stringify(ToolTabColumnPermisionsCanViewCanEditHasFilterGridViewModel.Rows);
                    console.log(DTO);
                    $.ajax({
                        url: urls["AccountSaveToolTabColumnPermisionsCanViewCanEditHasFilterData"],
                        type: 'POST',
                        contentType: 'application/json; charset=utf-8',
                        data: DTO,
                        success: function (data) {
                            if (data.indexOf("Error") != -1)
                                jAlert(data, "Error");
                        },
                        error: function (xhr) {
                            jAlert(xhr.statusText, "Error");
                        },
                        complete: function () {
                            LineNoArray = []
                            $(".LineNo").each(function (index, elem) {
                                LineNoArray.push($(elem).val())
                            });
                            $('#ToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                            jAlert("Data SuccessFully Saved:", "Successful");
                        }
                    });
                }
            }
            break;
        case 3:
            {
                var ToolTabColumnPermisionsCanViewCanEditGridViewModel = {
                    Rows: [],
                };
                var ind = 0;
                $(".LineNoChangeDropHide").each(function (index, elem) {
                    var trparent = $(elem).parent();
                    var tdparent = trparent.parent();
                    var View = $(tdparent.find("input.CanView:checkbox")).prop("checked");
                    var Edit = $(tdparent.find("input.CanEdit:checkbox")).prop("checked");
                    var SaveRows = {
                        UserID: $("#UserID").val(),
                        ProfileID: 0,
                        ToolTabID: $("#ToolTabID").val(),
                        SelectedToolTabColumnID: parseInt($(elem).val()),
                        CanView: View,
                        CanEdit: Edit,
                    };
                    ToolTabColumnPermisionsCanViewCanEditGridViewModel.Rows.push(SaveRows);
                    ++ind;
                });
                var DTO = JSON.stringify(ToolTabColumnPermisionsCanViewCanEditGridViewModel.Rows);
                console.log(DTO);
                $.ajax({
                    url: urls["AccountSaveToolTabColumnPermisionsCanViewCanEditData"],
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    data: DTO,
                    success: function (data) {
                        if (data.indexOf("Error") != -1)
                            jAlert(data, "Error");
                    },
                    error: function (xhr) {
                        jAlert(xhr.statusText, "Error");
                    },
                    complete: function () {
                        $('#ToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                        jAlert("Data SuccessFully Saved:", "Successful");
                    }
                });
            }
            break;
        case 4:
            {
                var ToolTabColumnPermisionsCanViewGridViewModel = {
                    Rows: [],
                };
                var ind = 0;
                $(".LineNoChangeDropHide").each(function (index, elem) {
                    var trparent = $(elem).parent();
                    var tdparent = trparent.parent();
                    var View = $(tdparent.find("input.CanView:checkbox")).prop("checked");
                    var SaveRows = {
                        UserID: $("#UserID").val(),
                        ProfileID: 0,
                        ToolTabID: $("#ToolTabID").val(),
                        SelectedToolTabColumnID: parseInt($(elem).val()),
                        CanView: View,
                    };
                    ToolTabColumnPermisionsCanViewGridViewModel.Rows.push(SaveRows);
                    ++ind;
                });
                var DTO = JSON.stringify(ToolTabColumnPermisionsCanViewGridViewModel.Rows);
                console.log(DTO);
                $.ajax({
                    url: urls["AccountSaveToolTabColumnPermisionsCanViewData"],
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    data: DTO,
                    success: function (data) {
                        if (data.indexOf("Error") != -1)
                            jAlert(data, "Error");
                    },
                    error: function (xhr) {
                        jAlert(xhr.statusText, "Error");
                    },
                    complete: function () {
                        $('#ToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                        jAlert("Data SuccessFully Saved:", "Successful");
                    }
                });
            }
            break;
        default:
            jAlert("You don`t chose any Tool:", "Warning!");
            break;

    }
    
});
$(document).on('click', '#ClearLineNumDiv', function () {
    if (!$("#ClearLineNoCheckbox").is(':Disabled')) {
        $("#ClearLineNoCheckbox").prop('checked', !($("#ClearLineNoCheckbox").is(':checked')));
        if ($("#ClearLineNoCheckbox").prop("checked")) {
            $(".LineNo").each(function (index, elem) {
                $(elem).val(null)
            });
        }
        else {
            $(".LineNo").each(function (index, elem) {
                $(elem).val(LineNoArray[index]);
            });
        }
    }
});
$(document).on('change', '#ClearLineNoCheckbox', function () {
    $(this).prop('checked', !($(this).is(':checked')));
    if ($(this).prop("checked")) {
        $(".LineNo").each(function (index, elem) {
            $(elem).val(null)
        });
    }
    else {
        $(".LineNo").each(function (index, elem) {
            $(elem).val(LineNoArray[index]);
        });
    }
});
$(document).on('click', '#ManualLineNoDiv', function () {
    $("#ManualLineNoCheckbox").prop('checked', !($('#ManualLineNoCheckbox').is(':checked')));
    if ($("#ManualLineNoCheckbox").prop("checked")) {
        $("#ClearLineNoCheckbox").attr("Disabled", false);
        $(".LineNo").each(function (index, elem) {
            $(elem).attr("ReadOnly", false)
        });
        $(".ToolTabColumnNameDrop").each(function (index, elem) {
            $(elem).prop("disabled", true);
        });
    }
    else {
        $("#ClearLineNoCheckbox").prop("checked", false);
        $("#ClearLineNoCheckbox").attr("Disabled", true);
        $(".LineNo").each(function (index, elem) {
            $(elem).attr("ReadOnly", true);
            $(elem).val(LineNoArray[index]);
        });
        $(".ToolTabColumnNameDrop").each(function (index, elem) {
            $(elem).prop("disabled", false);
        });
    }
});
$(document).on('change', '#ManualLineNoCheckbox', function () {
    $(this).prop('checked', !($(this).is(':checked')));
    if ($(this).prop("checked"))
    {
        $("#ClearLineNoCheckbox").attr("Disabled", false);
        $(".LineNo").each(function (index, elem) {
            $(elem).attr("ReadOnly", false)
        });
        $(".ToolTabColumnNameDrop").each(function (index, elem) {
            $(elem).prop( "disabled", true );
        });
    }
    else
    {
        $("#ClearLineNoCheckbox").prop("checked", false);
        $("#ClearLineNoCheckbox").attr("Disabled", true);
        $(".LineNo").each(function (index, elem) {
            $(elem).attr("ReadOnly", true);
            $(elem).val(LineNoArray[index]);
        });
        $(".ToolTabColumnNameDrop").each(function (index, elem) {
            $(elem).prop("disabled", false);
        });
    }
});
$(document).on('click', '#ProfileClearLineNumDiv', function () {
    if (!$("#ProfileClearLineNoCheckbox").is(':Disabled')) {
        $("#ProfileClearLineNoCheckbox").prop('checked', !($("#ProfileClearLineNoCheckbox").is(':checked')));
        if ($("#ProfileClearLineNoCheckbox").prop("checked")) {
            $(".ProfileLineNo").each(function (index, elem) {
                $(elem).val(null)
            });
        }
        else {
            $(".ProfileLineNo").each(function (index, elem) {
                $(elem).val(ProfileLineNoArray[index]);
            });
        }
    }
});
$(document).on('change', '#ProfileClearLineNoCheckbox', function () {
    $(this).prop('checked', !($(this).is(':checked')));
    if ($(this).prop("checked")) {
        $(".ProfileLineNo").each(function (index, elem) {
            $(elem).val(null)
        });
    }
    else {
        $(".ProfileLineNo").each(function (index, elem) {
            $(elem).val(ProfileLineNoArray[index]);
        });
    }
});
$(document).on('click', '#ProfileManualLineNoDiv', function () {
    $("#ProfileManualLineNoCheckbox").prop('checked', !($('#ProfileManualLineNoCheckbox').is(':checked')));
    if ($("#ProfileManualLineNoCheckbox").prop("checked")) {
        $("#ProfileClearLineNoCheckbox").attr("Disabled", false);
        $(".ProfileLineNo").each(function (index, elem) {
            $(elem).attr("ReadOnly", false)
        });
        $(".ProfileToolTabColumnNameDrop").each(function (index, elem) {
            $(elem).prop("disabled", true);
        });
    }
    else {
        $("#ProfileClearLineNoCheckbox").prop("checked", false);
        $("#ProfileClearLineNoCheckbox").attr("Disabled", true);
        $(".ProfileLineNo").each(function (index, elem) {
            $(elem).attr("ReadOnly", true);
            $(elem).val(ProfileLineNoArray[index]);
        });
        $(".ProfileToolTabColumnNameDrop").each(function (index, elem) {
            $(elem).prop("disabled", false);
        });
    }
});
$(document).on('change', '#ProfileManualLineNoCheckbox', function () {
    $(this).prop('checked', !($(this).is(':checked')));
    if ($(this).prop("checked")) {
        $("#ProfileClearLineNoCheckbox").attr("Disabled", false);
        $(".ProfileLineNo").each(function (index, elem) {
            $(elem).attr("ReadOnly", false)
        });
        $(".ProfileToolTabColumnNameDrop").each(function (index, elem) {
            $(elem).prop("disabled", true);
        });
    }
    else {
        $("#ProfileClearLineNoCheckbox").prop("checked", false);
        $("#ProfileClearLineNoCheckbox").attr("Disabled", true);
        $(".ProfileLineNo").each(function (index, elem) {
            $(elem).attr("ReadOnly", true);
            $(elem).val(ProfileLineNoArray[index]);
        });
        $(".ProfileToolTabColumnNameDrop").each(function (index, elem) {
            $(elem).prop("disabled", false);
        });
    }
});
$(document).on('click', '#ProfileToolTabColumnPermissionsSaveBtn', function () {
    if (LoadedToolTabColumnPermissionGrid && parseInt($("#ProfileToolID").val()) == 2) {
        if (ValidProfileToolTabColumnPermisionsCanViewCanEditHasFilterData()) {
            var $form = $('#LoadProfilePermissionsForm');
            var ErrorMessage = "";
            if ($form.valid()) {
                AccountValidationClientSideHide = false;
                $.ajax({
                    url: urls["AccountSaveProfile"],
                    data: {
                        'ProfileID': parseInt(ProfileIDView),
                        'ProfileName': $("#ProfileName").val(),
                    },
                    cache: false,
                    type: "POST",
                    success: function (data) {
                        ErrorMessage = data;
                        if (data != null || data != "") {
                            jAlert(data, "Error!");
                        }
                    },
                    error: function (reponse) {
                        jAlert(reponse, "Error");
                    },
                    complete: function () {
                        if (ErrorMessage == null || ErrorMessage == "") {
                            UpdateProfiles(parseInt(ProfileIDView));
                            $('#ProfileToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                            ProfileNameChange = false;
                            GridElementsChange = false;
                            SaveProfilePermissionsData();
                            jAlert("Data SuccessFully Saved:", "Successful");
                        }
                    }
                });
            }
            else {
                AccountValidationClientSideHide = true;
                $("#errorMessageServer").hide();
            }
        }
    }
    else {
        var $form = $('#LoadProfilePermissionsForm');
        var ErrorMessage = "";
        if ($form.valid()) {
            AccountValidationClientSideHide = false;
            $.ajax({
                url: urls["AccountSaveProfile"],
                data: {
                    'ProfileID': parseInt(ProfileIDView),
                    'ProfileName': $("#ProfileName").val(),
                },
                cache: false,
                type: "POST",
                success: function (data) {
                    ErrorMessage = data;
                    if (data != null || data != "") {
                        jAlert(data, "Error!");
                    }
                },
                error: function (reponse) {
                    jAlert(reponse, "Error");
                },
                complete: function () {
                    if (ErrorMessage == null || ErrorMessage == "") {
                        UpdateProfiles(parseInt(ProfileIDView));
                        $('#ProfileToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                        ProfileNameChange = false;
                        GridElementsChange = false;
                        SaveProfilePermissionsData();
                        jAlert("Data SuccessFully Saved:", "Successful");
                    }
                }
            });
        }
        else {
            AccountValidationClientSideHide = true;
            $("#errorMessageServer").hide();
        }
    }

});
function SaveProfilePermissionsData() {
    if (LoadedToolTabColumnPermissionGrid) {
        if ($("#LoadProfilePermissionsForm").valid()) {
            switch (parseInt($("#ProfileToolID").val())) {
                case 1:
                    {
                        if ($("#ProfileToolTabID").val() == 17) {
                            var ToolTabColumnPermisionsCanViewCanEditGridViewModel = {
                                Rows: [],
                            };
                            var ind = 0;
                            $(".ProfileLineNoChangeDropHide").each(function (index, elem) {
                                var trparent = $(elem).parent();
                                var tdparent = trparent.parent();
                                var View = $(tdparent.find("input.ProfileCanView:checkbox")).prop("checked");
                                var Edit = $(tdparent.find("input.ProfileCanEdit:checkbox")).prop("checked");
                                var SaveRows = {
                                    UserID: 0,
                                    ProfileID: parseInt(ProfileIDView),
                                    ProfileName: $("#ProfileName").val(),
                                    ToolTabID: $("#ProfileToolTabID").val(),
                                    SelectedToolTabColumnID: parseInt($(elem).val()),
                                    CanView: View,
                                    CanEdit: Edit,
                                };
                                ToolTabColumnPermisionsCanViewCanEditGridViewModel.Rows.push(SaveRows);
                                ++ind;
                            });
                            var DTO = JSON.stringify(ToolTabColumnPermisionsCanViewCanEditGridViewModel.Rows);
                            console.log(DTO);
                            $.ajax({
                                url: urls["AccountSaveToolTabColumnPermisionsCanViewCanEditData"],
                                type: 'POST',
                                contentType: 'application/json; charset=utf-8',
                                data: DTO,
                                success: function (data) {
                                    if (data.indexOf("Error") != -1)
                                        jAlert(data, "Error");
                                },
                                error: function (xhr) {
                                    jAlert(xhr.statusText, "Error");
                                },
                                complete: function () {
                                    UpdateProfiles(parseInt(ProfileIDView));
                                }
                            });
                        }
                        else {
                            var ToolTabColumnPermisionsCanViewGridViewModel = {
                                Rows: [],
                            };
                            var ind = 0;
                            $(".ProfileLineNoChangeDropHide").each(function (index, elem) {
                                var trparent = $(elem).parent();
                                var tdparent = trparent.parent();
                                var View = $(tdparent.find("input.ProfileCanView:checkbox")).prop("checked");
                                var SaveRows = {
                                    UserID: 0,
                                    ProfileID: parseInt(ProfileIDView),
                                    ProfileName: $("#ProfileName").val(),
                                    ToolTabID: $("#ProfileToolTabID").val(),
                                    SelectedToolTabColumnID: parseInt($(elem).val()),
                                    CanView: View,
                                };
                                ToolTabColumnPermisionsCanViewGridViewModel.Rows.push(SaveRows);
                                ++ind;
                            });
                            var DTO = JSON.stringify(ToolTabColumnPermisionsCanViewGridViewModel.Rows);
                            console.log(DTO);
                            $.ajax({
                                url: urls["AccountSaveToolTabColumnPermisionsCanViewData"],
                                type: 'POST',
                                contentType: 'application/json; charset=utf-8',
                                data: DTO,
                                success: function (data) {
                                    if (data.indexOf("Error") != -1)
                                        jAlert(data, "Error");
                                },
                                error: function (xhr) {
                                    jAlert(xhr.statusText, "Error");
                                },
                                complete: function () {
                                    UpdateProfiles(parseInt(ProfileIDView));
                                }
                            });
                        }
                    }
                    break;
                case 2:
                    {
                        var ToolTabColumnPermisionsCanViewCanEditHasFilterGridViewModel = {
                            Rows: [],
                        };
                        var ind = 0;
                        $(".ProfileLineNoChangeDropHide").each(function (index, elem) {
                            var trparent = $(elem).parent();
                            var tdparent = trparent.parent();
                            var LineNum = $(tdparent.find(".ProfileLineNo")).val();
                            var View = $(tdparent.find("input.ProfileCanView:checkbox")).prop("checked");
                            var Edit = $(tdparent.find("input.ProfileCanEdit:checkbox")).prop("checked");
                            var Filter = $(tdparent.find("input.ProfileHasFilter:checkbox")).prop("checked");
                            var SaveRows = {
                                UserID: 0,
                                ProfileID: parseInt(ProfileIDView),
                                ProfileName: $("#ProfileName").val(),
                                ToolTabFixedColumnCountID: $("#ProfileToolTabFixedColumnCountID").val(),
                                ToolTabID: $("#ProfileToolTabID").val(),
                                FixedColumnCount: $("#ProfileFixedColumnCount").val(),
                                SelectedToolTabColumnID: parseInt($(elem).val()),
                                LineNo: parseInt(LineNum),
                                CanView: View,
                                CanEdit: Edit,
                                HasFilter: Filter,
                            };
                            ToolTabColumnPermisionsCanViewCanEditHasFilterGridViewModel.Rows.push(SaveRows);
                            ++ind;
                        });
                        var DTO = JSON.stringify(ToolTabColumnPermisionsCanViewCanEditHasFilterGridViewModel.Rows);
                        console.log(DTO);
                        $.ajax({
                            url: urls["AccountSaveToolTabColumnPermisionsCanViewCanEditHasFilterData"],
                            type: 'POST',
                            contentType: 'application/json; charset=utf-8',
                            data: DTO,
                            success: function (data) {
                                if (data.indexOf("Error") != -1)
                                    jAlert(data, "Error");
                            },
                            error: function (xhr) {
                                jAlert(xhr.statusText, "Error");
                            },
                            complete: function () {
                                UpdateProfiles(parseInt(ProfileIDView));
                                ProfileLineNoArray = []
                                $(".ProfileLineNo").each(function (index, elem) {
                                    ProfileLineNoArray.push($(elem).val())
                                });
                            }
                        });
                    }
                    break;
                case 3:
                    {
                        var ToolTabColumnPermisionsCanViewCanEditGridViewModel = {
                            Rows: [],
                        };
                        var ind = 0;
                        $(".ProfileLineNoChangeDropHide").each(function (index, elem) {
                            var trparent = $(elem).parent();
                            var tdparent = trparent.parent();
                            var View = $(tdparent.find("input.ProfileCanView:checkbox")).prop("checked");
                            var Edit = $(tdparent.find("input.ProfileCanEdit:checkbox")).prop("checked");
                            var SaveRows = {
                                UserID: 0,
                                ProfileID: parseInt(ProfileIDView),
                                ProfileName: $("#ProfileName").val(),
                                ToolTabID: $("#ProfileToolTabID").val(),
                                SelectedToolTabColumnID: parseInt($(elem).val()),
                                CanView: View,
                                CanEdit: Edit,
                            };
                            ToolTabColumnPermisionsCanViewCanEditGridViewModel.Rows.push(SaveRows);
                            ++ind;
                        });
                        var DTO = JSON.stringify(ToolTabColumnPermisionsCanViewCanEditGridViewModel.Rows);
                        console.log(DTO);
                        $.ajax({
                            url: urls["AccountSaveToolTabColumnPermisionsCanViewCanEditData"],
                            type: 'POST',
                            contentType: 'application/json; charset=utf-8',
                            data: DTO,
                            success: function (data) {
                                if (data.indexOf("Error") != -1)
                                    jAlert(data, "Error");
                            },
                            error: function (xhr) {
                                jAlert(xhr.statusText, "Error");
                            },
                            complete: function () {
                                UpdateProfiles(parseInt(ProfileIDView));
                            }
                        });
                    }
                    break;
                case 4:
                    {
                        var ToolTabColumnPermisionsCanViewGridViewModel = {
                            Rows: [],
                        };
                        var ind = 0;
                        $(".ProfileLineNoChangeDropHide").each(function (index, elem) {
                            var trparent = $(elem).parent();
                            var tdparent = trparent.parent();
                            var View = $(tdparent.find("input.ProfileCanView:checkbox")).prop("checked");
                            var SaveRows = {
                                UserID: 0,
                                ProfileID: parseInt(ProfileIDView),
                                ProfileName: $("#ProfileName").val(),
                                ToolTabID: $("#ProfileToolTabID").val(),
                                SelectedToolTabColumnID: parseInt($(elem).val()),
                                CanView: View,
                            };
                            ToolTabColumnPermisionsCanViewGridViewModel.Rows.push(SaveRows);
                            ++ind;
                        });
                        var DTO = JSON.stringify(ToolTabColumnPermisionsCanViewGridViewModel.Rows);
                        console.log(DTO);
                        $.ajax({
                            url: urls["AccountSaveToolTabColumnPermisionsCanViewData"],
                            type: 'POST',
                            contentType: 'application/json; charset=utf-8',
                            data: DTO,
                            success: function (data) {
                                if (data.indexOf("Error") != -1)
                                    jAlert(data, "Error");
                            },
                            error: function (xhr) {
                                jAlert(xhr.statusText, "Error");
                            },
                            complete: function () {
                                UpdateProfiles(parseInt(ProfileIDView));
                            }
                        });
                    }
                    break;
                default:
                    jAlert("You don`t chose any Tool:", "Warning!");
                    break;
            }

        }
    }
}
function UpdateProfiles(SelectedProfile) {
    $.ajax({
        url: urls["AccountUpdateProfiles"],
        cache: false,
        type: "POST",
        success: function (data) {
            var markup = "";
            for (var x = 0; x < data.length; x++) {
                var markup =
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
            }
            $("#ProfileSelectedProfile").html(markup).show();
            $("#SelectedProfile").html(markup).show();
        },
        error: function (reponse) {
            jAlert(reponse, "Error");
        },
        complete: function () {
            $("#ProfileSelectedProfile").val(SelectedProfile);
            $("#SelectedProfile").val(SelectedProfile);
            if ($("#ProfileApplyBtn").hidden) {
                $("#ProfileApplyBtn").show();
            }
            if (parseInt(ProfileIDView) != 0) {
                $("#ProfileName").val($('option:selected', $("#ProfileSelectedProfile")).text());
            }
            else {
                ProfileIDView = $("#ProfileSelectedProfile").prop("selectedIndex", 0).val();
                $("#ProfileID").val(ProfileIDView);
            }
        }
    });
}
$(document).on('change', '.ToolDrop', function () {
    var $ToolDropLoc = $(this);
    if ($('#ToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
            if (retVal == true) {
                $("#ToolTabColumnPermissionsGridDiv").empty();
                $("#ToolTabColumnPermissionsGridDiv").hide();
                if ($ToolDropLoc.val() === "0" || $ToolDropLoc.val() === "") {
                    $("#tooltabdrop").hide();
                }
                else
                {
                    $.ajax({
                        url: urls["AccountGetToolTabByToolID"],
                        data: { ToolID: parseInt($ToolDropLoc.val()) },
                        cache: false,
                        type: "POST",
                        success: function (data) {
                            var markup = "<option value='0'>Select</option>";
                            for (var x = 0; x < data.length; x++) {
                                markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                            }
                            $("Select.ToolTabDrop").html(markup).show();
                        },
                        error: function (reponse) {
                            jAlert(reponse, "Error");
                        },
                        complete: function () {
                            $("#tooltabdrop").show();
                        }
                    });
                }
                ToolDropJs = $ToolDropLoc.val();
                $('#ToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
            }
            else {
                $ToolDropLoc.val(ToolDropJs);
            }
        });
    }
    else {
        $("#ToolTabColumnPermissionsGridDiv").empty();
        $("#ToolTabColumnPermissionsGridDiv").hide();
        if ($ToolDropLoc.val() === "0" || $ToolDropLoc.val() === "") {
            $("#tooltabdrop").hide();
        }
        else {
            $.ajax({
                url: urls["AccountGetToolTabByToolID"],
                data: { ToolID: parseInt($ToolDropLoc.val()) },
                cache: false,
                type: "POST",
                success: function (data) {
                    var markup = "<option value='0'>Select</option>";
                    for (var x = 0; x < data.length; x++) {
                        markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                    }
                    $("Select.ToolTabDrop").html(markup).show();
                },
                error: function (reponse) {
                    jAlert(reponse, "Error");
                },
                complete: function () {
                    $("#tooltabdrop").show();
                }
            });
        }
        ToolDropJs = $ToolDropLoc.val();
    }
});
$(document).on('change', '.ProfileToolDrop', function () {
    var $ProfileToolDropLoc = $(this);
    if ($('#ProfileToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed') && GridElementsChange) {
        jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
            if (retVal == true) {
                LoadedToolTabColumnPermissionGrid = false;
                $("#ProfileToolTabColumnPermissionsGridDiv").empty();
                $("#ProfileToolTabColumnPermissionsGridDiv").hide();
                $("#CopyAllProfile").hide();
                if ($ProfileToolDropLoc.val() === "0" || $ProfileToolDropLoc.val() === "") {
                    $("#Profiletooltabdrop").hide();
                }
                else {
                    $.ajax({
                        url: urls["AccountGetToolTabByToolID"],
                        data: { ToolID: parseInt($ProfileToolDropLoc.val()) },
                        cache: false,
                        type: "POST",
                        success: function (data) {
                            var markup = "<option value='0'>Select</option>";
                            for (var x = 0; x < data.length; x++) {
                                markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                            }
                            $("Select.ProfileToolTabDrop").html(markup).show();
                        },
                        error: function (reponse) {
                            jAlert(reponse, "Error");
                        },
                        complete: function () {
                            $("#Profiletooltabdrop").show();
                        }
                    });
                }
                ProfileToolDropJs = $ProfileToolDropLoc.val();
                if (!ProfileNameChange) {
                    $('#ProfileToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                }
                GridElementsChange = false;
            }
            else {
                $ProfileToolDropLoc.val(ProfileToolDropJs);
            }
        });
    }
    else {
        LoadedToolTabColumnPermissionGrid = false;
        $("#ProfileToolTabColumnPermissionsGridDiv").empty();
        $("#ProfileToolTabColumnPermissionsGridDiv").hide();
        $("#CopyAllProfile").hide();
        if ($ProfileToolDropLoc.val() === "0" || $ProfileToolDropLoc.val() === "") {
            $("#Profiletooltabdrop").hide();
        }
        else {
            $.ajax({
                url: urls["AccountGetToolTabByToolID"],
                data: { ToolID: parseInt($ProfileToolDropLoc.val()) },
                cache: false,
                type: "POST",
                success: function (data) {
                    var markup = "<option value='0'>Select</option>";
                    for (var x = 0; x < data.length; x++) {
                        markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                    }
                    $("Select.ProfileToolTabDrop").html(markup).show();
                },
                error: function (reponse) {
                    jAlert(reponse, "Error");
                },
                complete: function () {
                    $("#Profiletooltabdrop").show();
                }
            });
        }
        ProfileToolDropJs = $ProfileToolDropLoc.val();
    }
});
$(document).on('click', '#CreateProfile', function () {
    if ($('#ProfileToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
            if (retVal == true) {
                jQuery('label.error').each(function (currentElement, index) {
                    $(this).remove();
                });
                LoadedToolTabColumnPermissionGrid = false;
                ProfileIDViewChange = ProfileIDView;
                $("#ProfileID").val(ProfileIDView);
                ProfileIDView = "0";
                $("#ProfileID").val(ProfileIDView);
                $("#ProfileName").addClass('Created');
                $("#ProfileName").val('');
                $("#CreateProfile").hide();
                $("#errorMessageClient").text('');
                $("#errorMessageServer").text('');
                $("#EditeProfile").show();
                $("select#ProfileSelectedProfile").prop("disabled", true);
                $("#Profiletooldrop").show();
                $("select#SelectedProfileTool").val('');
                $("#ProfileToolTabColumnPermissionsGridDiv").empty();
                $("#ProfileToolTabColumnPermissionsGridDiv").hide();
                $("#CopyAllProfile").hide();
                $("#Profiletooltabdrop").hide();
                $('#ProfileToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                ProfileNameChange = false;
                GridElementsChange = false;
            }
        });
    }
    else
    {
        LoadedToolTabColumnPermissionGrid = false;
        ProfileIDViewChange = ProfileIDView;
        $("#ProfileID").val(ProfileIDView);
        ProfileIDView = "0";
        $("#ProfileID").val(ProfileIDView);
        $("#ProfileName").addClass('Created');
        $("#ProfileName").val('');
        $("#CreateProfile").hide();
        $("#errorMessageClient").text('');
        $("#errorMessageServer").text('');
        $("#EditeProfile").show();
        $("select#ProfileSelectedProfile").prop("disabled", true);
        $("#Profiletooldrop").show();
        $("select#SelectedProfileTool").val('');
        $("#ProfileToolTabColumnPermissionsGridDiv").empty();
        $("#ProfileToolTabColumnPermissionsGridDiv").hide();
        $("#CopyAllProfile").hide();
        $("#Profiletooltabdrop").hide();
    }
});
$(document).on('click', '#EditeProfile', function () {
    if ($('#ProfileToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
            if (retVal == true) {
                jQuery('label.error').each(function (currentElement, index) {
                    $(this).remove();
                });
                LoadedToolTabColumnPermissionGrid = false;
                $("#errorMessageClient").text('');
                ProfileIDView = ProfileIDViewChange;
                $("#ProfileID").val(ProfileIDView);
                $("#EditeProfile").hide();
                $("select#SelectedProfileTool").val('');
                $("#ProfileToolTabColumnPermissionsGridDiv").empty();
                $("#ProfileToolTabColumnPermissionsGridDiv").hide();
                $("#CopyAllProfile").hide();
                $("#Profiletooltabdrop").hide();
                $("#ProfileName").removeClass('Created');
                $("#CreateProfile").show();
                UpdateProfiles(parseInt(ProfileIDView));
                $("select#ProfileSelectedProfile").prop("disabled", false);
                $('#ProfileToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed')
                ProfileNameChange = false;
                GridElementsChange = false;
            }
        });
    }
    else
    {
        LoadedToolTabColumnPermissionGrid = false;
        $("#errorMessageClient").text('');
        ProfileIDView = ProfileIDViewChange;
        $("#ProfileID").val(ProfileIDView);
        $("#EditeProfile").hide();
        $("select#SelectedProfileTool").val('');
        $("#ProfileToolTabColumnPermissionsGridDiv").empty();
        $("#ProfileToolTabColumnPermissionsGridDiv").hide();
        $("#CopyAllProfile").hide();
        $("#Profiletooltabdrop").hide();
        $("#ProfileName").removeClass('Created');
        $("#CreateProfile").show();
        UpdateProfiles(parseInt(ProfileIDView));
        $("select#ProfileSelectedProfile").prop("disabled", false);
    }
  
});
$(document).on('change', '.ProfileDrop', function () {
    if ($('#ProfileToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
            if (retVal == true) {
                LoadedToolTabColumnPermissionGrid = false;
                $("#ProfileToolTabColumnPermissionsGridDiv").empty();
                $("#ProfileToolTabColumnPermissionsGridDiv").hide();
                $("#CopyAllProfile").hide();
                $("#Profiletooltabdrop").hide();
                $("select#SelectedProfileTool").val('');
                $("select#SelectedProfileToolTab").val('');
                if ($("select#ProfileSelectedProfile").val() === "") {
                    ProfileIDView = "0";
                    $("#ProfileID").val(ProfileIDView);
                    $("#ProfileName").val('');
                    $("#Profiletool").hide();
                }
                else {
                    ProfileIDView = $("select#ProfileSelectedProfile").val();
                    $("#ProfileID").val(ProfileIDView);
                    $("#ProfileName").val($('option:selected', $("select#ProfileSelectedProfile")).text());
                    $("#Profiletool").show();
                }
                $('#ProfileToolTabColumnPermissionsSaveBtn').removeClass('BackColorRed');
                ProfileNameChange = false;
                GridElementsChange = false;
            }
            else
            {
                $("select#ProfileSelectedProfile").val(ProfileIDView);
            }
        });
    }
    else
    {
        LoadedToolTabColumnPermissionGrid = false;
        $("#ProfileToolTabColumnPermissionsGridDiv").empty();
        $("#ProfileToolTabColumnPermissionsGridDiv").hide();
        $("#CopyAllProfile").hide();
        $("#Profiletooltabdrop").hide();
        $("select#SelectedProfileTool").val('');
        $("select#SelectedProfileToolTab").val('');
        if ($("select#ProfileSelectedProfile").val() === "") {
            ProfileIDView = "0";
            $("#ProfileID").val(ProfileIDView);
            $("#ProfileName").val('');
            $("#Profiletool").hide();
        }
        else {
            ProfileIDView = $("select#ProfileSelectedProfile").val();
            $("#ProfileID").val(ProfileIDView);
            $("#ProfileName").val($('option:selected', $("select#ProfileSelectedProfile")).text());
            $("#Profiletool").show();
        }
    }
   
});
function BindToolTabColumnName() {
    $(".ToolTabColumnNameDrop").each(function (index, elem) {
        $(elem).next().prop("selectedIndex", $(elem).prop("selectedIndex"));
    });
}
function BindProfileToolTabColumnName() {
    $(".ProfileToolTabColumnNameDrop").each(function (index, elem) {
        $(elem).next().prop("selectedIndex", $(elem).prop("selectedIndex"));
    });
}
$(document).on('change', '.CheckBox', function () {
    if ($(this).hasClass('CanView')) {
        BindCheckBoxHeader('CanView');
    }
    if ($(this).hasClass('CanEdit')) {
        BindCheckBoxHeader('CanEdit');
    }
    if ($(this).hasClass('HasFilter')) {
        BindCheckBoxHeader('HasFilter');
    }
}); 
function MakeDisabledHasFilterCanEdit()
{
    var CanViewStates = $("#ToolTabColumnPermissionsGridDiv input.CanView:checkbox");
    var IsDisabled = true;
    if($('#ToolTabColumnPermissionsGridDiv #AllowViewCheckbox').prop("checked"))
    {
        IsDisabled = false
    }
    else
    {
        var CanViewStates = $("#ToolTabColumnPermissionsGridDiv input.CanView:checkbox");
        $(CanViewStates).each(function (index, elem) {
            CanViewChangeEvent(elem);
            if ($(elem).prop('checked')) {
                IsDisabled = false;
            }
        });
    }
}
function BindCheckBoxHeader(Checkbox)
{
    var CheckBoxStates = $("#ToolTabColumnPermissionsGridDiv input." + Checkbox + ":checkbox");
    var IsChecked = true;
    $(CheckBoxStates).each(function (index, elem) {
        if (!$(elem).is(':disabled')) {
            if (!$(elem).prop('checked')) {
                IsChecked = false;
            }
        }
    });
    switch (Checkbox)
    {
        case 'CanView':
            {
                $('#ToolTabColumnPermissionsGridDiv #AllowViewCheckbox').prop("checked", IsChecked);
            }
            break;
        case 'CanEdit':
            {
                var CanEditCheckBoxStates = $("#ToolTabColumnPermissionsGridDiv input.CanEdit:checkbox");
                var IsDisabled = true;
                $(CanEditCheckBoxStates).each(function (index, elem) {
                    if (!$(elem).is(':disabled')) {
                        IsDisabled = false
                    }
                });
                if (IsDisabled) {
                    $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').prop('checked', !IsDisabled);
                    $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').attr("disabled", true);
                }
                else {
                    $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').removeAttr("disabled");
                    $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').prop("checked", IsChecked);
                }
            }
            break;
        case 'HasFilter':
            {
                var HasFilterCheckBoxStates = $("#ToolTabColumnPermissionsGridDiv input.HasFilter:checkbox");
                var IsDisabled = true;
                $(HasFilterCheckBoxStates).each(function (index, elem) {
                    if (!$(elem).is(':disabled')) {
                        IsDisabled = false
                    }
                });
                if (IsDisabled) {
                    $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').prop('checked', !IsDisabled);
                    $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').attr("disabled", true);
                }
                else {
                    $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').removeAttr("disabled");
                    $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').prop("checked", IsChecked);
                }
            }
            break;
        default:
            break;
    }
}
$(document).on('click', '.CanViewChange', function () {
    CanViewChangeEvent(this);
});
function CanViewChangeEvent(element)
{
    var tr = $(element).parent().parent();
    var IsChecked = $(element).prop("checked")
    if (!IsChecked) {
        if ($(tr).find('input.CanEdit:checkbox').length > 0) {
            $(tr).find('input.CanEdit:checkbox').prop('checked', IsChecked);
            $(tr).find('input.CanEdit:checkbox').attr("disabled", true);
            BindCheckBoxHeader('CanEdit');
        }
    }
    else if ($(tr).find('input.CanEdit:checkbox').length > 0)
    {
        $(tr).find('input.CanEdit:checkbox').removeAttr("disabled");
        BindCheckBoxHeader('CanEdit');
    }
    if (!IsChecked) {
        if ($(tr).find('input.HasFilter:checkbox').length > 0) {
            $(tr).find('input.HasFilter:checkbox').prop('checked', IsChecked);
            $(tr).find('input.HasFilter:checkbox').attr("disabled", true);
            BindCheckBoxHeader('HasFilter');
        }
    }
    else if ($(tr).find('input.HasFilter:checkbox').length > 0) {
        $(tr).find('input.HasFilter:checkbox').removeAttr("disabled");
        BindCheckBoxHeader('HasFilter');
    }
}
$(document).on('click', '.AllowViewth', function () {
    $('#ToolTabColumnPermissionsGridDiv #AllowViewCheckbox').prop('checked', !($('#ToolTabColumnPermissionsGridDiv #AllowViewCheckbox').is(':checked')));
    var CanView = $("#ToolTabColumnPermissionsGridDiv input.CanView:checkbox");
    var IsChecked = $('#ToolTabColumnPermissionsGridDiv #AllowViewCheckbox').prop("checked");
    $(CanView).each(function (index, elem) {
        $(elem).prop('checked', IsChecked)
        CanViewChangeEvent(elem);
    });
    if ($("#ToolTabID").val() != 17) {
        if (!IsChecked) {
            if ($('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').length > 0) {
                $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').prop('checked', IsChecked);
                $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').attr("disabled", true);
            }
        }
        else if ($('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').length > 0) {
            $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').removeAttr("disabled");
        }
        if (!IsChecked) {
            if ($('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').length > 0) {
                $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').prop('checked', IsChecked);
                $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').attr("disabled", true);
            }
        }
        else if ($('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').length > 0) {
            $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').removeAttr("disabled");
        }
    }
    $('#ToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
});
$(document).on('change', '#AllowViewCheckbox', function () {
    $('#ToolTabColumnPermissionsGridDiv #AllowViewCheckbox').prop('checked', !($('#ToolTabColumnPermissionsGridDiv #AllowViewCheckbox').is(':checked')));
    var CanView = $("#ToolTabColumnPermissionsGridDiv input.CanView:checkbox");
    var IsChecked = !($('#ToolTabColumnPermissionsGridDiv #AllowViewCheckbox').prop("checked"));
    $(CanView).each(function (index, elem) {
        $(elem).prop('checked', IsChecked)
        CanViewChangeEvent(elem);

    });
    if ($("#ToolTabID").val() != 17) {
        if (!IsChecked) {
            if ($('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').length > 0) {
                $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').prop('checked', IsChecked);
                $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').attr("disabled", true);
            }
        }
        else if ($('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').length > 0) {
            $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').removeAttr("disabled");
        }
        if (!IsChecked) {
            if ($('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').length > 0) {
                $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').prop('checked', IsChecked);
                $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').attr("disabled", true);
            }
        }
        else if ($('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').length > 0) {
            $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').removeAttr("disabled");
        }
    }
    $('#ToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
});
$(document).on('click', '.AllowEditth', function () {
    if (!$('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').is(':disabled')) {
        $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').prop('checked', !($('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').is(':checked')));
        var CanEdit = $("#ToolTabColumnPermissionsGridDiv input.CanEdit:checkbox");
        var IsChecked = $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').prop("checked");
        $(CanEdit).each(function (index, elem) {
            if (!$(elem).is(':disabled')) {
                $(elem).prop('checked', IsChecked)
            }
        });
        $('#ToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
    }
});
$(document).on('change', '#AllowEditCheckbox', function () {
    $('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').prop('checked', !($('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').is(':checked')));
    var CanEdit = $("#ToolTabColumnPermissionsGridDiv input.CanEdit:checkbox");
    var IsChecked = !($('#ToolTabColumnPermissionsGridDiv #AllowEditCheckbox').prop("checked"));
    $(CanEdit).each(function (index, elem) {
        if (!$(elem).is(':disabled')) {
            $(elem).prop('checked', IsChecked)
        }
    });
    $('#ToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
});
$(document).on('click', '.HasFilterth', function () {
    if (!$('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').is(':disabled')) {
        $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').prop('checked', !($('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').is(':checked')));
        var HasFilter = $("#ToolTabColumnPermissionsGridDiv input.HasFilter:checkbox");
        var IsChecked = $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').prop("checked");
        $(HasFilter).each(function (index, elem) {
            if (!$(elem).is(':disabled')) {
                $(elem).prop('checked', IsChecked)
            }
        });
        $('#ToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
    }
});
$(document).on('change', '#HasFilterCheckbox', function () {
    $('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').prop('checked', !($('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').is(':checked')));
    var HasFilter = $("#ToolTabColumnPermissionsGridDiv input.HasFilter:checkbox");
    var IsChecked = !($('#ToolTabColumnPermissionsGridDiv #HasFilterCheckbox').prop("checked"));
    $(HasFilter).each(function (index, elem) {
        if (!$(elem).is(':disabled')) {
            $(elem).prop('checked', IsChecked)
        }
    });
    $('#ToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
});
$(document).on('change', '.ProfileCheckBox', function () {
    if($(this).hasClass('ProfileCanView'))    {
        BindProfileCheckBoxHeader('ProfileCanView');
    }
    if ($(this).hasClass('ProfileCanEdit')) {
        BindProfileCheckBoxHeader('ProfileCanEdit');
    }
    if ($(this).hasClass('ProfileHasFilter')) {
        BindProfileCheckBoxHeader('ProfileHasFilter');
    }
});
function ProfileMakeDisabledHasFilterCanEdit() {
    var CanViewStates = $("#ProfileToolTabColumnPermissionsGridDiv input.ProfileCanView:checkbox");
    var IsDisabled = true;
    if ($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowViewCheckbox').prop("checked")) {
        IsDisabled = false
    }
    else {
        var CanViewStates = $("#ProfileToolTabColumnPermissionsGridDiv input.ProfileCanView:checkbox");
        $(CanViewStates).each(function (index, elem) {
            ProfileCanViewChangeEvent(elem);
            if ($(elem).prop('checked')) {
                IsDisabled = false;
            }
        });
    }
}
function BindProfileCheckBoxHeader(Checkbox) {
    var CheckBoxStates = $("#ProfileToolTabColumnPermissionsGridDiv input." + Checkbox + ":checkbox");
    var IsChecked = true;
    $(CheckBoxStates).each(function (index, elem) {
        if (!$(elem).is(':disabled')) {
            if (!$(elem).prop('checked')) {
                IsChecked = false;
            }
        }
    });
    switch (Checkbox) {
        case 'ProfileCanView':
            {
                $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowViewCheckbox').prop("checked", IsChecked);
            }
            break;
        case 'ProfileCanEdit':
            {
                var CanEditCheckBoxStates = $("#ProfileToolTabColumnPermissionsGridDiv input.ProfileCanEdit:checkbox");
                var IsDisabled = true;
                $(CanEditCheckBoxStates).each(function (index, elem) {
                    if (!$(elem).is(':disabled')) {
                        IsDisabled = false
                    }
                });
                if (IsDisabled) {
                    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').prop('checked', !IsDisabled);
                    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').attr("disabled", true);
                }
                else {
                    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').removeAttr("disabled");
                    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').prop("checked", IsChecked);
                }
            }
            break;
        case 'ProfileHasFilter':
            {
                var HasFilterCheckBoxStates = $("#ProfileToolTabColumnPermissionsGridDiv input.ProfileHasFilter:checkbox");
                var IsDisabled = true;
                $(HasFilterCheckBoxStates).each(function (index, elem) {
                    if (!$(elem).is(':disabled')) {
                        IsDisabled = false
                    }
                });
                if (IsDisabled) {
                    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').prop('checked', !IsDisabled);
                    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').attr("disabled", true);
                }
                else {
                    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').removeAttr("disabled");
                    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').prop("checked", IsChecked);
                }
            }
            break;
        default:
            break;
    }
}
$(document).on('click', '.ProfileCanViewChange', function () {
    ProfileCanViewChangeEvent(this);
});
function ProfileCanViewChangeEvent(element) {
    var tr = $(element).parent().parent();
    var IsChecked = $(element).prop("checked")
    if (!IsChecked) {
        if ($(tr).find('input.ProfileCanEdit:checkbox').length > 0) {
            $(tr).find('input.ProfileCanEdit:checkbox').prop('checked', IsChecked);
            $(tr).find('input.ProfileCanEdit:checkbox').attr("disabled", true);
            BindProfileCheckBoxHeader('ProfileCanEdit');
        }
    }
    else if ($(tr).find('input.ProfileCanEdit:checkbox').length > 0) {
        $(tr).find('input.ProfileCanEdit:checkbox').removeAttr("disabled");
        BindProfileCheckBoxHeader('ProfileCanEdit');
    }
    if (!IsChecked) {
        if ($(tr).find('input.ProfileHasFilter:checkbox').length > 0) {
            $(tr).find('input.ProfileHasFilter:checkbox').prop('checked', IsChecked);
            $(tr).find('input.ProfileHasFilter:checkbox').attr("disabled", true);
            BindProfileCheckBoxHeader('ProfileHasFilter');
        }
    }
    else if ($(tr).find('input.ProfileHasFilter:checkbox').length > 0) {
        $(tr).find('input.ProfileHasFilter:checkbox').removeAttr("disabled");
        BindProfileCheckBoxHeader('ProfileHasFilter');
    }
}
$(document).on('click', '.ProfileAllowViewth', function () {
    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowViewCheckbox').prop('checked', !($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowViewCheckbox').is(':checked')));
    var ProfileCanView = $("#ProfileToolTabColumnPermissionsGridDiv input.ProfileCanView:checkbox");
    var IsChecked = $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowViewCheckbox').prop("checked");
    $(ProfileCanView).each(function (index, elem) {
        $(elem).prop('checked', IsChecked)
        ProfileCanViewChangeEvent(elem);
    });
    if ($("#ProfileToolTabID").val() != 17) {
        if (!IsChecked) {
            if ($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').length > 0) {
                $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').prop('checked', IsChecked);
                $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').attr("disabled", true);
            }
        }
        else if ($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').length > 0) {
            $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').removeAttr("disabled");
        }
        if (!IsChecked) {
            if ($('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').length > 0) {
                $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').prop('checked', IsChecked);
                $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').attr("disabled", true);
            }
        }
        else if ($('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').length > 0) {
            $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').removeAttr("disabled");
        }
    }
    $('#ProfileToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
    GridElementsChange = true;
});
$(document).on('change', '#ProfileAllowViewCheckbox', function () {
    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowViewCheckbox').prop('checked', !($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowViewCheckbox').is(':checked')));
    var ProfileCanView = $("#ProfileToolTabColumnPermissionsGridDiv input.ProfileCanView:checkbox");
    var IsChecked = !($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowViewCheckbox').prop("checked"));
    $(ProfileCanView).each(function (index, elem) {
        $(elem).prop('checked', IsChecked)
        ProfileCanViewChangeEvent(elem);
    });
    if ($("#ProfileToolTabID").val() != 17) {
        if (!IsChecked) {
            if ($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').length > 0) {
                $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').prop('checked', IsChecked);
                $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').attr("disabled", true);
            }
        }
        else if ($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').length > 0) {
            $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').removeAttr("disabled");
        }
        if (!IsChecked) {
            if ($('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').length > 0) {
                $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').prop('checked', IsChecked);
                $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').attr("disabled", true);
            }
        }
        else if ($('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').length > 0) {
            $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').removeAttr("disabled");
        }
    }
    $('#ProfileToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
    GridElementsChange = true;
});
$(document).on('click', '.ProfileAllowEditth', function () {
    if (!$('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').is(':disabled')) {
        $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').prop('checked', !($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').is(':checked')));
        var ProfileCanEdit = $("#ProfileToolTabColumnPermissionsGridDiv input.ProfileCanEdit:checkbox");
        var IsChecked = $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').prop("checked");
        $(ProfileCanEdit).each(function (index, elem) {
            if (!$(elem).is(':disabled')) {
                $(elem).prop('checked', IsChecked)
            }
        });
        $('#ProfileToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
        GridElementsChange = true;
    }
});
$(document).on('change', '#ProfileAllowEditCheckbox', function () {
    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').prop('checked', !($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').is(':checked')));
    var ProfileCanEdit = $("#ProfileToolTabColumnPermissionsGridDiv input.ProfileCanEdit:checkbox");
    var IsChecked = !($('#ProfileToolTabColumnPermissionsGridDiv #ProfileAllowEditCheckbox').prop("checked"));
    $(ProfileCanEdit).each(function (index, elem) {
        if (!$(elem).is(':disabled')) {
            $(elem).prop('checked', IsChecked)
        }
    });
    $('#ProfileToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
    GridElementsChange = true;
});
$(document).on('click', '.ProfileHasFilterth', function () {
    if (!$('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').is(':disabled')) {
        $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').prop('checked', !($('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').is(':checked')));
        var ProfileHasFilter = $("#ProfileToolTabColumnPermissionsGridDiv input.ProfileHasFilter:checkbox");
        var IsChecked = $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').prop("checked");
        $(ProfileHasFilter).each(function (index, elem) {
            if (!$(elem).is(':disabled')) {
                $(elem).prop('checked', IsChecked)
            }
        });
        $('#ProfileToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
        GridElementsChange = true;
    }
});
$(document).on('change', '#ProfileHasFilterCheckbox', function () {
    $('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').prop('checked', !($('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').is(':checked')));
    var ProfileHasFilter = $("#ProfileToolTabColumnPermissionsGridDiv input.ProfileHasFilter:checkbox");
    var IsChecked = !($('#ProfileToolTabColumnPermissionsGridDiv #ProfileHasFilterCheckbox').prop("checked"));
    $(ProfileHasFilter).each(function (index, elem) {
        if (!$(elem).is(':disabled')) {
            $(elem).prop('checked', IsChecked)
        }
    });
    $('#ProfileToolTabColumnPermissionsSaveBtn').addClass('BackColorRed');
    GridElementsChange = true;
});
function LoadUserGrid() {
    var ManageUsersGridViewModel = {
        IsDelete: IsDeleteView,
        UserID: parseInt(UserIDView),
    };

    var DTO = JSON.stringify(ManageUsersGridViewModel);
    $.ajax({
        url: urls["AccountLoadUsersGrid"],
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: DTO,
        success: function (data) {
            $("#ManageUsersGrid").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            IsDeleteView = false;
        }
    });
}
$(document).on('change', '.CountryDrop', function () {
    FormattedPhoneNumbers($(this).val());
    if ($(this).val() === "") {
        $("#StateDiv").hide();
        $("#CityDiv").hide();
        $("#StateDivChange").show();
        $("#CityDivChange").show();
    }
    if ($(this).val() === '1') {
        $("#CityDiv").hide();
        $("#CityDivChange").show();
    }
    if ($(this).val() === '1') {
        $.ajax({
            url: urls["AccountGetStateByCountryID"],
            data: { CountryID: parseInt($(this).val()) },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $("Select.StateDrop").html(markup);
            },
            error: function (reponse) {
                jAlert(reponse, "Error");
            },
            complete: function () {
                $("#StateDiv").show();
                $("#StateDivChange").hide();
            }
        });
    }
    else if ($(this).val() != "") {
        $.ajax({
            url: urls["AccountGetCityByCountryID"],
            data: { CountryID: parseInt($(this).val()), StateID: '0' },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $("Select.CityDrop").html(markup);
            },
            error: function (reponse) {
                jAlert(reponse, "Error");
            },
            complete: function () {
                $("#StateDiv").hide();
                $("#StateDivChange").show();
                $("#CityDiv").show();
                $("#CityDivChange").hide();
            }
        });
    }
});
$(document).on('change', '.StateDrop', function () {
    var StateID = $(this).val();
    $.ajax({
        url: urls["AccountGetCityByCountryID"],
        data: { CountryID: 0, StateID: StateID },
        cache: false,
        type: "POST",
        success: function (data) {
            var markup = "<option value='0'>Select</option>";
            for (var x = 0; x < data.length; x++) {
                markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
            }
            $("Select.CityDrop").html(markup);
        },
        error: function (reponse) {
            jAlert(reponse, "Error");
        },
        complete: function () {
            if (StateID === "" || StateID === "0") {
                $("#CityDiv").hide();
                $("#CityDivChange").show();
            }
            else {
                $("#CityDiv").show();
                $("#CityDivChange").hide();
            }
        }
    });
});
$(document).on('change', '#HomePhone', function () {
    HomePhone = $(this).val();
});
$(document).on('change', '#EmrgcyContactPhone', function () {
    EmrgcyContactPhone = $(this).val();
});
$(document).on('change', '#PhoneNumberUnformatted', function () {
    if ($("#PhoneNumberUnformatted").prop('checked') == true) {
        $(".phone").unmask();
        $(".phoneUSACAN").unmask();
        $(".phoneTR").unmask();
        $(".phoneFR").unmask();
        $(".phoneBEL").unmask();
        $(".phoneSWI").unmask();
        $(".phoneTS").unmask();
        $(".phoneALG").unmask();
        $(".phoneMA").unmask();

        $("#HomePhone").addClass("number");
        $("#EmrgcyContactPhone").addClass("number");
        $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });

        $("#HomePhone").val(HomePhone);
        $("#EmrgcyContactPhone").val(EmrgcyContactPhone);
    }
    else {
        $(".number").unmask();
        $("#HomePhone").removeClass("number");
        $("#EmrgcyContactPhone").removeClass("number");
        FormattedPhoneNumbers($("#SelectedLCountryID").val())
    }
});
function FormattedPhoneNumbers(CountryID) {
    if ($("#PhoneNumberUnformatted").prop('checked') != true) {

        $("#HomePhone").val(HomePhone);
        $("#EmrgcyContactPhone").val(EmrgcyContactPhone);

        switch (CountryID) {
            case '1': case '7':
                {
                    if (SelectedLCountryID == '1' || SelectedLCountryID == '7') {
                        $("#HomePhone").val(HomePhone);
                        $("#EmrgcyContactPhone").val(EmrgcyContactPhone);
                    }
                    $("#HomePhone").removeAttr('class');
                    $("#HomePhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneUSACAN');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneUSACAN');
                    $(".phoneUSACAN").mask("+1 (###) ###-####");
                };
                break;
            case '3':
                {
                    $("#HomePhone").removeAttr('class');
                    $("#HomePhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneTR');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneTR');
                    $(".phoneTR").mask("+90 (###) ###-####");
                };
                break;
            case '5':
                {
                    $("#HomePhone").removeAttr('class');
                    $("#HomePhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneFR');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneFR');
                    $(".phoneFR").mask("+33 (#) ##-##-##-##");
                };
                break
            case '8':
                {
                    $("#HomePhone").removeAttr('class');
                    $("#HomePhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneBEL');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneBEL');
                    $(".phoneBEL").mask("+32 (#) ###-##-##");
                };
                break
            case '11':
                {
                    $("#HomePhone").removeAttr('class');
                    $("#HomePhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneSWI');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneSWI');
                    $(".phoneSWI").mask("+41 (##) ###-####");
                };
                break
            case '12':
                {
                    $("#HomePhone").removeAttr('class');
                    $("#HomePhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneTS');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneTS');
                    $(".phoneTS").mask("+216 (##) ###-###");
                };
                break
            case '24':
                {
                    $("#HomePhone").removeAttr('class');
                    $("#HomePhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneALG');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneALG');
                    $(".phoneALG").mask("+213 (##) ###-####");
                };
                break
            case '26':
                {
                    $("#HomePhone").removeAttr('class');
                    $("#HomePhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneMA');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phoneMA');
                    $(".phoneMA").mask("+212 (##) ###-####");
                };
                break
            default:
                {
                    $("#HomePhone").removeAttr('class');
                    $("#HomePhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phone');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('table_inputs_SmallPadding2 inpputgr form-control UserDetailsElementChange phone');
                    $(".phone").mask("(###) ###-####");
                };
        }
    }
}

var originalClose = $.colorbox.close;
$.colorbox.close = function () {
    if ($('#UserDetailsSaveBtn').hasClass('BackColorRed') || $('#ToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed') || $('#ProfileToolTabColumnPermissionsSaveBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ClosePopUp"], Messages["ClosePopUpTitle"], function (retVal) {
            if (retVal == true) {
                $("#UserDetailsDiv").empty();
                $("#tabPermissions").empty();
                $("#tabProfiles").empty();
                $("#tabPermissions").removeClass("active in");
                $("#tabProfiles").removeClass("active in");
                $("#Permissions").removeClass("active");
                $("#Profiles").removeClass("active");
                originalClose();
                LoadUserGrid();
            }
        });
    }
    else {
        $("#UserDetailsDiv").empty();
        $("#tabPermissions").empty();
        $("#tabProfiles").empty();
        $("#tabPermissions").removeClass("active in");
        $("#tabProfiles").removeClass("active in");
        $("#Permissions").removeClass("active");
        $("#Profiles").removeClass("active");
        originalClose();
        LoadUserGrid();
    }
};
$(document).on('change', '.ElementsForValidation', function () {
    if (window.location.pathname.indexOf("Login") == -1) {
        if ($(this).valid()) {
            $(this).closest("form").find("#errorMessageClient").text("");
        }
        else
            $(this).closest("form").find("#errorMessageServer").hide();
        if (AccountValidationClientSideHide) {
            if ($(this).closest("form").valid()) {
                $(this).closest("form").find("#errorMessageClient").text("");
            }
        }
        else if ($(this).valid()) {
            var elements = "#" + $(this).closest("form").attr("id") + " .error";
            $(elements).not('label').each(function () {
                $(this).valid();
            });
        }
    }
    else if (AccountValidationClientSideHide) {
        if ($(this).closest("form").valid()) {
            $(this).closest("form").find("#errorMessageClient").text("");
        }
    }
});